package com.sxz.sbs.other;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.sxz.sbs.R;
import com.sxz.sbs.app.GlideApp;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片宫格展示
 */

public class ImageShowAdapter extends RecyclerView.Adapter<ImageShowAdapter.ImageShowHolder> {
    private Context context;
    private List<FileBean> imgList = new ArrayList<>();
    private int MAX_COUNT = 3;
    private int imageSize;
    private OnImageShowListener mOnImageShowListener;

    public ImageShowAdapter(Context context, List<FileBean> imgList, int maxCount) {
        this.context = context;
        if (imgList.size() < maxCount) {
            imgList.add(null);
        }
        this.imgList = imgList;
        this.MAX_COUNT = maxCount;
        setColumnNumber(context, 3);


    }

    public void setOnImageShowListener(OnImageShowListener onImageShowListener) {
        this.mOnImageShowListener = onImageShowListener;
    }

    @Override
    public ImageShowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_img_show_layout, null);
        return new ImageShowHolder(view);
    }

    /**
     * 设置图片宽高
     *
     * @param context
     * @param i
     */
    private void setColumnNumber(Context context, int i) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        int widthPixels = metrics.widthPixels;
        imageSize = widthPixels / i;
    }

    @Override
    public void onBindViewHolder(ImageShowHolder holder, final int position) {
        if (imgList.get(position) != null) {
            String path = imgList.get(position).getPath();
            holder.ivDel.setVisibility(View.VISIBLE);
            GlideApp.with(context).load(path).override(imageSize, imageSize).placeholder(R.drawable.icon_place_holder).into(holder.ivShow);
        } else {
            holder.ivDel.setVisibility(View.GONE);
            GlideApp.with(context).load(R.drawable.icon_add_img).override(imageSize, imageSize).into(holder.ivShow);
        }

        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnImageShowListener != null) {
                    mOnImageShowListener.onImageDelete(v, position);
                }
            }
        });
        holder.ivShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnImageShowListener != null) {
                    if (imgList.get(position) != null) {
                        mOnImageShowListener.onImageClick(v, position);
                    } else {
                        mOnImageShowListener.onImageAdd(v, position);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (this.imgList != null) {
            return this.imgList.size();
        }
        return 0;
    }

    public void setNotify(List<FileBean> imgList) {
        if (imgList == null) {
            return;
        }
        if (imgList.size() < MAX_COUNT && imgList.get(0) != null && !imgList.contains(null)) {
            imgList.add(null);
        }
        this.imgList = imgList;
        notifyDataSetChanged();
    }

    class ImageShowHolder extends RecyclerView.ViewHolder {
        ImageView ivShow;
        ImageView ivDel;

        public ImageShowHolder(View itemView) {
            super(itemView);
            ivShow = (ImageView) itemView.findViewById(R.id.item_iv_show);
            ivDel = (ImageView) itemView.findViewById(R.id.item_iv_del);
        }
    }

    public interface OnImageShowListener {
        /**
         * 点击图片
         */
        void onImageClick(View v, int position);

        /**
         * 点击删除
         *
         * @param v
         * @param position
         */
        void onImageDelete(View v, int position);

        /**
         * 点击添加
         */
        void onImageAdd(View v, int position);
    }
}
